package org.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lease.mapper.ViewAppointmentMapper;
import org.lease.model.dto.AppointmentDTO;
import org.lease.model.entity.ViewAppointment;
import org.lease.model.enums.AppointmentStatus;
import org.lease.model.query.AppointmentPageQuery;
import org.lease.model.vo.ApartmentItemVo;
import org.lease.model.vo.AppointmentDetailVo;
import org.lease.model.vo.AppointmentItemVo;
import org.lease.service.ApartmentInfoService;
import org.lease.service.ViewAppointmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment> implements ViewAppointmentService {
    @Autowired
    private ApartmentInfoService apartmentInfoService;

    // 分页条件查询预约信息
    @Override
    public IPage<AppointmentDTO> pageQueryAppointment(AppointmentPageQuery query) {
        Page<AppointmentDTO> page = new Page<>(query.getCurrent(), query.getSize());
        return baseMapper.pageQueryAppointment(page, query);
    }

    // 更新预约信息的状态
    @Override
    public boolean updateAppointmentStatus(Long id, AppointmentStatus status) {
        LambdaUpdateWrapper<ViewAppointment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ViewAppointment::getId, id);
        wrapper.set(ViewAppointment::getAppointmentStatus, status);
        return update(wrapper);
    }

    @Override
    public List<AppointmentItemVo> listItem(Long userId) {
        return baseMapper.listItem(userId);
    }

    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        ViewAppointment viewAppointment = baseMapper.selectById(id);
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(viewAppointment.getApartmentId());

        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment, appointmentDetailVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        return appointmentDetailVo;
    }
}
